//@author A0085897A

import java.io.File;
import java.util.List;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Workbook;
import jxl.Sheet;
import jxl.WorkbookSettings;

public class FileImport {

	// ATTRIBUTES
	private String _filename;
	private Transactions _transactions;
	private Workbook _workbook;

	// CONSTRUCTOR
	public FileImport(String file) throws Exception{

		this._filename = (file.endsWith(".xls") || file.endsWith(".xlsx")) ? file : file + ".xls";
		WorkbookSettings settings = new WorkbookSettings();
		settings.setSuppressWarnings(true);
		this._workbook = Workbook.getWorkbook(new File(file), settings);
		this._transactions = new Transactions();
		this.readFile();

	}

	// METHODS
	// Getters
	public String getFilename() {
		return this._filename;
	}
	public List<Transaction> getTransactions() {
		return this._transactions.getTransactions();
	}
	public Workbook getWorkbook() {
		return this._workbook;
	}
	public Sheet getSheet() {
		return this.getWorkbook().getSheet(0);
	}
	// Read file
	public void readFile() {
		LabelCell lc;
		NumberCell nc;
		String name, type, date, category, paymentMethod, remarks, status;
		double value;
		for (int i=1; i<this.getSheet().getRows(); i++) {
			lc = (LabelCell) this.getSheet().getCell(1,i);
			name = lc.getString();
			nc = (NumberCell) this.getSheet().getCell(2,i);
			value = nc.getValue();
			lc = (LabelCell) this.getSheet().getCell(3,i);
			date = lc.getString();
			lc = (LabelCell) this.getSheet().getCell(4,i);
			category = lc.getString();
			lc = (LabelCell) this.getSheet().getCell(5,i);
			paymentMethod = lc.getString();
			lc = (LabelCell) this.getSheet().getCell(6,i);
			remarks = lc.getString();
			lc = (LabelCell) this.getSheet().getCell(7,i);
			status = lc.getString();
			type = (value < 0) ? "EXPENSE" : "DEPOSIT";
			this.getTransactions().add(new Transaction(name, Math.abs(value), type, date, category, paymentMethod, remarks, status));
		}

	}

}
